package demo4.tools;

import demo4.tools.IDBUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//将@Component注解注释了，模拟将这个类文件从项目中移除了(不需要容器管理了)
//@Component
@PropertySource("classpath:config.properties")
public class DBUtil implements IDBUtil {
    @Value("${spring.datasource.classname}")
    private String className;
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    @Override
    public Connection getConnection() {
        System.out.println("DBUtil:返回一个数据库连接");
        try {
            Class.forName(className);
            return DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}



